﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models.PlatForm.Dtos.DbSync;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 数据同步
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.08.31
    /// </summary>
    [Route("api/PlatForm/[controller]")]
    public class DbSyncController : BaseApiController
    {
        private DbSyncBll dbSyncBll = new DbSyncBll();
        private DbLinkBll dbLinkBll = new DbLinkBll();

        #region POST
        /// <summary>
        /// 执行同步
        /// </summary>
        /// <param name="postParam">请求参数</param>
        /// <returns></returns>
        [HttpPost("Actions/Execute")]
        public async Task<ResponseResult> Execute([FromBody] DbSyncActionsExecuteInput postParam)
        {
            var linkTo = await dbLinkBll.GetInfo(postParam.dbConnectionTo.ToString());
            if (dbSyncBll.IsNullDataByTable(linkTo, postParam.dbTable))
            {
                return Error("目的表存在数据，无法同步");
            }
            var data = await dbSyncBll.ImportTableData(postParam.dbConnectionFrom.ToString(), postParam.dbConnectionTo.ToString(), postParam.dbTable.ToString());
            if (data == "ok")
            {
                return Success("同步成功");
            }
            else
            {
                return Error("同步失败", data);
            }
        }
        #endregion
    }
}
